package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseThreadLocal;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    public R<AddressBook> save(@RequestBody AddressBook addressBook) {
        //获取userid
        Long userId = BaseThreadLocal.get();
        addressBook.setUserId(userId);
        boolean save = addressBookService.save(addressBook);
        if (save){
            return R.success(addressBook);
        }
        return R.error("保存失败");
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
        //先将该用户所有地址的is_default更新为0
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper<>();
        luw.eq(AddressBook::getId,BaseThreadLocal.get());
        luw.set(AddressBook::getIsDefault,0);
        addressBookService.update(luw);
        //设置默认地址为1,是
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null){
            return R.success(addressBook);
        }
        return R.error("没有找到该对象");
    }

    /**
     * 查询默认地址
     */
    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getId,BaseThreadLocal.get());
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(qw);
        if (addressBook != null){
            return R.success(null);
        }else {
            return R.error("没有找到该对象");
        }

    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(AddressBook addressBook) {
        //根据当前登录用户ID，查询所有的地址列表 select * from address_book where user_id =  ?
        //获取当前用户id
        Long id = BaseThreadLocal.get();
        addressBook.setUserId(id);//指定用户
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        //先判断指定用户id是为空
        qw.eq(addressBook.getUserId() != null,AddressBook::getUserId,addressBook.getUserId());
        //再根据修改时间排序
        qw.orderByDesc(AddressBook::getUpdateTime);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }

    /**
     * 删除地址(可能是多个)
     */
    @DeleteMapping
    public R deleteAddress(@RequestParam List<Long> ids){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId,ids);
        addressBookService.remove(qw);
        LambdaQueryWrapper<AddressBook> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(AddressBook::getId,ids);
        addressBookService.remove(qw1);
        return R.success("");
    }

    /**
    * 修改地址
    * */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        boolean update = addressBookService.updateById(addressBook);
        if (update){
            return R.success("修改成功");
        }else {
            return R.error("编辑失败");
        }

    }
}
